At first, I install the package animint2 and load it.
if(!requireNamespace("animint2")) install.packages("animint2")
Loading required namespace: animint2
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Warning message:
R graphics engine version 16 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.
library(animint2)
I will load the dataset WorldBank for visualization
data(WorldBank, package="animint2")
# Remove " (all income levels)" in region field
WorldBank$Region <- sub(" (all income levels)", "", WorldBank$region, fixed=TRUE)
# Plot some final records of the dataset
tail(WorldBank)
# How many rows and columns in this datasets?
dim(WorldBank)
[1] 11342 16
# Filter out records in 1975
WorldBank1975 <- subset(WorldBank, year==1975)
# Plot some early found records in 1975
head(WorldBank1975)
# Plot the relationship between life expectancy and fertility rate of countries in 1975
scatter <- ggplot()+
geom_point(
mapping=aes(x=life.expectancy, y=fertility.rate, color=Region),
data=WorldBank1975)
scatter
animint(scatter)
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(1) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file174316addce93 at http://127.0.0.1:4321
# This block visualizes the trend of fertility rate and life expectancy between 1970 and 1975
WorldBankBefore1975 <- subset(WorldBank, 1970 <= year & year <= 1975)
two.layers <- scatter+
geom_path(aes(
x=life.expectancy,
y=fertility.rate,
color=Region,
group=country),
data=WorldBankBefore1975)
two.layers
(viz.two.layers <- animint(two.layers))
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(2) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file17431488d5c40 at http://127.0.0.1:3345
viz.two.layers
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(3) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file1743122605383 at http://127.0.0.1:3011
# This command adds the name of countries to the plot as an overlay layer
three.layers <- two.layers+
geom_text(aes(
x=life.expectancy,
y=fertility.rate,
color=Region,
label=country),
data=WorldBank1975)
three.layers
animint(three.layers)
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(4) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file17431486160d0 at http://127.0.0.1:3342
# Now we assign a new element named `timeSeries` to visualize the trend of fertility rate over years
viz.two.plots <- viz.two.layers
viz.two.plots$timeSeries <- ggplot()+
geom_line(aes(
x=year,
y=fertility.rate,
color=Region,
group=country),
data=WorldBank)
# After assigned, `viz.two.plots` has two elements: twolayers and timeSeries
summary(viz.two.plots)
Length Class Mode
twolayers 9 gganimint list
timeSeries 9 gganimint list
# Draw all the plots
viz.two.plots$twolayers
viz.two.plots$timeSeries
viz.two.plots
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(5) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file1743124c62292 at http://127.0.0.1:4498
# In this exercise, I re-use the above `viz.two.plots` and add a new element named `population` to the end.
# The new element will reveal the changes in population by country.
viz.three.plots <- viz.two.plots
viz.three.plots$population <- ggplot()+
geom_point(aes(
x=year,
y=population,
color=Region),
data=WorldBankBefore1975)
# After assigned, `viz.three.plots` has three elements: twolayers and timeSeries
summary(viz.three.plots)
Length Class Mode
twolayers 9 gganimint list
timeSeries 9 gganimint list
population 9 gganimint list
# Draw all the plots
viz.three.plots$twolayers
viz.three.plots$timeSeries
viz.three.plots$population
viz.three.plots
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(6) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file1743168384f49 at http://127.0.0.1:5156
# In case we want to make the same y-axis for all plots
add.x.var <- function(df, x.var){
data.frame(df, x.var=factor(x.var, c("life expectancy", "year")))
}
(viz.aligned <- animint(
scatter=ggplot()+
theme_animint(width=1000)+
geom_point(aes(x=life.expectancy, y=fertility.rate, color=Region),
data=add.x.var(WorldBank1975, "life expectancy"))+
geom_path(aes(x=life.expectancy, y=fertility.rate, color=Region,
group=country),
data=add.x.var(WorldBankBefore1975, "life expectancy"))+
geom_line(aes(x=year, y=fertility.rate, color=Region, group=country),
data=add.x.var(WorldBank, "year"))+
xlab("")+
facet_grid(. ~ x.var, scales="free")+
theme_bw()+
theme(panel.margin=grid::unit(0, "lines"))
))
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(7) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file1743175c4d729 at http://127.0.0.1:6019
viz.aligned$scatter
# I create a table that contains data in three years: 1975, 1985, 1995
show.point.list <- list()
show.path.list <- list()
for(show.year in c(1975, 1985, 1995)){
show.point.list[[paste(show.year)]] <- data.frame(
show.year, subset(WorldBank, year==show.year))
show.path.list[[paste(show.year)]] <- data.frame(
show.year, subset(WorldBank, show.year - 5 <= year & year <= show.year))
}
show.point <- do.call(rbind, show.point.list)
show.path <- do.call(rbind, show.path.list)
# Now we will visualize the relationship between fertility rate and life expectancy in (1970-1975), (1980-1985), and (1990-1995).
viz.panels <- list(
scatter=ggplot()+
geom_point(aes(x=life.expectancy, y=fertility.rate, color=Region),
data=show.point)+
geom_path(aes(x=life.expectancy, y=fertility.rate, color=Region,
group=country),
data=show.path)+
facet_grid(. ~ show.year)+
theme_bw()+
theme(panel.margin=grid::unit(0, "lines"))
)
viz.panels$scatter
structure(viz.panels, class="animint")
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(8) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/RtmpDmhYLw/file174312c61fdc2 at http://127.0.0.1:3703